﻿//--------------------------------------------------------------------
// All Rights Reserved , Copyright (C) 2011 , Hairihan TECH, Ltd. 
//--------------------------------------------------------------------

using System;
using System.Data;
using System.Drawing;
using System.Windows.Forms;

namespace DotNet.WinForm.Permission
{
    using DotNet.Model;
    using DotNet.Service;
    using DotNet.Utilities;
    using DotNet.WinForm.Utilities;

    /// <summary>
    /// FrmRoleOrganizeScope.cs
    /// 角色(用户组)数据集权限范围设置
    ///		
    /// 修改记录
    /// 
    ///     2011.02.26 版本：1.1 JiRiGaLa  默认加载数据集权限。
    ///     2010.12.08 版本：1.0 JiRiGaLa  模块配置。
    ///		
    /// 版本：1.1
    ///
    /// <author>
    ///		<name>JiRiGaLa</name>
    ///		<date>2011.02.26</date>
    /// </author> 
    /// </summary>
    public partial class FrmRoleOrganizeScope : BaseForm
    {
        public FrmRoleOrganizeScope()
        {
            InitializeComponent();
        }

        private string TargetPermissionId
        {
            get
            {
                return this.ucPermissionScope.SelectedId;
            }
            set
            {
                this.ucPermissionScope.SelectedId = value;
            }
        }

        /// <summary>
        /// 权限域（范围权限、数据集权限）
        /// </summary>
        private string permissionScopeCode = "SystemAdmin";

        /// <summary>
        /// 以组织机构为主的数据集权限
        /// </summary>
        DataTable dtRoleOrganizeScope = new DataTable(BaseRoleTable.TableName);

        private void ucPermissionScope_SelectedIndexChanged(string selectedId)
        {
            this.TargetPermissionId = selectedId;
            this.GetRoleOrganizeScope();
        }

        #region private void BindItemDetails() 绑定下拉筐数据
        /// <summary>
        /// 绑定下拉筐数据
        /// </summary>
        private void BindItemDetails()
        {
            // 绑定职位数据，这里需要考虑屏幕刷新、批量保存时的效果问题
            if (this.cmbRoleCategory.Items.Count == 0)
            {
                DataTable dataTable = ServiceManager.Instance.ItemDetailsService.GetDT(UserInfo, "Items_RoleCategory");
                DataRow dataRow = dataTable.NewRow();
                dataTable.Rows.InsertAt(dataRow, 0);
                this.cmbRoleCategory.DisplayMember = BaseItemDetailsTable.FieldItemName;
                this.cmbRoleCategory.ValueMember = BaseItemDetailsTable.FieldItemValue;
                this.cmbRoleCategory.DataSource = dataTable.DefaultView;
            }
        }
        #endregion

        #region private void SetRowFilter() 设置数据过滤
        /// <summary>
        /// 设置数据过滤
        /// </summary>
        private void SetRowFilter()
        {
            if (dtRoleOrganizeScope.Rows.Count > 0)
            {
                this.dtRoleOrganizeScope.DefaultView.RowFilter = this.GetCategoryFilter();
            }
        }
        #endregion

        private string GetCategoryFilter()
        {
            string returnValue = string.Empty;
            if (this.cmbRoleCategory.SelectedValue != null)
            {
                if (this.cmbRoleCategory.SelectedValue.ToString().Length > 0)
                {
                    returnValue = BaseRoleTable.FieldCategory + " = '" + this.cmbRoleCategory.SelectedValue + "' ";
                }
            }
            return returnValue;
        }

        private void cmbRoleCategory_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.FormLoaded)
            {
                // 设置数据过滤
                this.SetRowFilter();
                // 设置按钮状态
                this.SetControlState();
            }
        }

        public override void SetControlState()
        {
            this.btnBatchSave.Enabled = false;
            if (this.dtRoleOrganizeScope.Rows.Count > 0)
            {
                this.btnBatchSave.Enabled = true;
            }
        }

        private void GetRoleOrganizeScope()
        {
            // 1.获取角色列表
            // 自己能管理的角色列表才对，是否启用了权限范围管理
            if (BaseSystemInfo.UsePermissionScope)
            {
                dtRoleOrganizeScope = ServiceManager.Instance.PermissionService.GetRoleDTByPermission(this.UserInfo, UserInfo.Id, this.permissionScopeCode);
            }
            else
            {
                dtRoleOrganizeScope = ServiceManager.Instance.RoleService.GetDT(this.UserInfo);
            }

            // 2.设置数据集权限范围的列
            dtRoleOrganizeScope.Columns.Add("colAll", typeof(bool));
            dtRoleOrganizeScope.Columns.Add("colUserCompany", typeof(bool));
            dtRoleOrganizeScope.Columns.Add("colUserSubOrg", typeof(bool));
            dtRoleOrganizeScope.Columns.Add("colUserDepartment", typeof(bool));
            dtRoleOrganizeScope.Columns.Add("colUserWorkgroup", typeof(bool));
            dtRoleOrganizeScope.Columns.Add("colUser", typeof(bool));
            dtRoleOrganizeScope.Columns.Add("colNone", typeof(bool));
            dtRoleOrganizeScope.Columns.Add("colDetail", typeof(bool));

            // 3.获取数据集权限

            // 4.绑定数据
            string targetRoleId = string.Empty;
            for (int i = 0; i < dtRoleOrganizeScope.Rows.Count; i++)
            {
                targetRoleId = this.dtRoleOrganizeScope.Rows[i][BaseRoleTable.FieldId].ToString();
                string[] organizeIds = ServiceManager.Instance.PermissionService.GetRoleScopeOrganizeIds(UserInfo, targetRoleId, this.TargetPermissionId);
                if (BaseBusinessLogic.Exists(organizeIds, ((int)PermissionScope.None).ToString()))
                {
                    dtRoleOrganizeScope.Rows[i]["colNone"] = true;
                }
                if (BaseBusinessLogic.Exists(organizeIds, ((int)PermissionScope.Detail).ToString()))
                {
                    dtRoleOrganizeScope.Rows[i]["colDetail"] = true;
                }
                if (BaseBusinessLogic.Exists(organizeIds, ((int)PermissionScope.User).ToString()))
                {
                    dtRoleOrganizeScope.Rows[i]["colUser"] = true;
                }
                if (BaseBusinessLogic.Exists(organizeIds, ((int)PermissionScope.UserWorkgroup).ToString()))
                {
                    dtRoleOrganizeScope.Rows[i]["colUserWorkgroup"] = true;
                }
                if (BaseBusinessLogic.Exists(organizeIds, ((int)PermissionScope.UserDepartment).ToString()))
                {
                    dtRoleOrganizeScope.Rows[i]["colUserDepartment"] = true;
                }
                if (BaseBusinessLogic.Exists(organizeIds, ((int)PermissionScope.UserSubOrg).ToString()))
                {
                    dtRoleOrganizeScope.Rows[i]["colUserSubOrg"] = true;
                }
                if (BaseBusinessLogic.Exists(organizeIds, ((int)PermissionScope.UserCompany).ToString()))
                {
                    dtRoleOrganizeScope.Rows[i]["colUserCompany"] = true;
                }
                if (BaseBusinessLogic.Exists(organizeIds, ((int)PermissionScope.All).ToString()))
                {
                    dtRoleOrganizeScope.Rows[i]["colAll"] = true;
                }
            }

            // 5.表格绑定数据
            dtRoleOrganizeScope.DefaultView.Sort = BaseRoleTable.FieldSortCode;
            this.grdRole.DataSource = dtRoleOrganizeScope.DefaultView;

            // 6.设置排序按钮
            this.ucTableSort.DataBind(this.grdRole);

            // 设置数据过滤
            this.SetRowFilter();
            this.SetControlState();
        }

        public override void FormOnLoad()
        {
            // 获取分类列表
            this.BindItemDetails();
            if (string.IsNullOrEmpty(this.TargetPermissionId))
            {
                DataTable dtPermissionItem = ServiceManager.Instance.PermissionItemService.GetDT(UserInfo);
                dtPermissionItem.DefaultView.Sort = BasePermissionItemTable.FieldSortCode;
                // 只显示权限域的权限列表
                BaseBusinessLogic.SetFilter(dtPermissionItem, BasePermissionItemTable.FieldIsScope, "1");
                if (dtPermissionItem.Rows.Count > 0)
                {
                    BasePermissionItemEntity permissionItemEntity = new BasePermissionItemEntity(dtPermissionItem.Rows[0]);
                    this.ucPermissionScope.SelectedId = permissionItemEntity.Id.ToString();
                }
            }
            this.grdRole.AutoGenerateColumns = false;

            this.GetRoleOrganizeScope();

            this.SetControlState();
        }

        /// <summary>
        /// 获得当前选中的权限范围选项
        /// </summary>
        /// <param name="dataRow">当前数据集权限的设置</param>
        /// <returns>选中权限范围</returns>
        private PermissionScope GetPermissionScope(DataRow dataRow, out string revokePermissionScope)
        {
            revokePermissionScope = string.Empty;
            PermissionScope returnValue = PermissionScope.Min;//nick

            if (dataRow["colAll"] != DBNull.Value)
            {
                if ((bool)dataRow["colAll"])
                {
                    returnValue = PermissionScope.All;
                }
                else
                {
                    revokePermissionScope += ((int)PermissionScope.All).ToString() + ";";
                }
            }

            if (dataRow["colUserCompany"] != DBNull.Value)
            {
                if ((bool)dataRow["colUserCompany"])
                {
                    returnValue = PermissionScope.UserCompany;
                }
                else
                {
                    revokePermissionScope += ((int)PermissionScope.UserCompany).ToString() + ";";
                }
            }

            if (dataRow["colUserSubOrg"] != DBNull.Value)
            {
                if ((bool)dataRow["colUserSubOrg"])
                {
                    returnValue = PermissionScope.UserSubOrg;
                }
                else
                {
                    revokePermissionScope += ((int)PermissionScope.UserSubOrg).ToString() + ";";
                }
            }

            if (dataRow["colUserDepartment"] != DBNull.Value)
            {
                if ((bool)dataRow["colUserDepartment"])
                {
                    returnValue = PermissionScope.UserDepartment;
                }
                else
                {
                    revokePermissionScope += ((int)PermissionScope.UserDepartment).ToString() + ";";
                }
            }

            if (dataRow["colUserWorkgroup"] != DBNull.Value)
            {
                if ((bool)dataRow["colUserWorkgroup"])
                {
                    returnValue = PermissionScope.UserWorkgroup;
                }
                else
                {
                    revokePermissionScope += ((int)PermissionScope.UserWorkgroup).ToString() + ";";
                }
            }

            if (dataRow["colUser"] != DBNull.Value)
            {
                if ((bool)dataRow["colUser"])
                {
                    returnValue = PermissionScope.User;
                }
                else
                {
                    revokePermissionScope += ((int)PermissionScope.User).ToString() + ";";
                }
            }

            if (dataRow["colDetail"] != DBNull.Value)
            {
                if ((bool)dataRow["colDetail"])
                {
                    returnValue = PermissionScope.Detail;
                }
                else
                {
                    revokePermissionScope += ((int)PermissionScope.Detail).ToString() + ";";
                }
            }

            if (dataRow["colNone"] != DBNull.Value)
            {
                if ((bool)dataRow["colNone"])
                {
                    returnValue = PermissionScope.None;
                }
                else
                {
                    revokePermissionScope += ((int)PermissionScope.None).ToString() + ";";
                }
            }

            return returnValue;
        }

        public bool BatchSave()
        {
            string grantOrganizes = string.Empty;
            string revokeOrganizes = string.Empty;
            string targetRoleId = string.Empty;
            for (int i = 0; i < this.dtRoleOrganizeScope.Rows.Count; i++)
            {
                targetRoleId = this.dtRoleOrganizeScope.Rows[i][BaseRoleTable.FieldId].ToString();
                
                string[] grantOrganizeIds = null;
                string[] revokeOrganizeIds = null;

                grantOrganizes = string.Empty;
                revokeOrganizes = string.Empty;
                grantOrganizes = ((int)this.GetPermissionScope(this.dtRoleOrganizeScope.Rows[i], out revokeOrganizes)).ToString() + ";";
                if (grantOrganizes.Length > 1)
                {
                    grantOrganizes = grantOrganizes.Substring(0, grantOrganizes.Length - 1);
                    grantOrganizeIds = grantOrganizes.Split(';');
                }
                if (revokeOrganizes.Length > 1)
                {
                    revokeOrganizes = revokeOrganizes.Substring(0, revokeOrganizes.Length - 1);
                    revokeOrganizeIds = revokeOrganizes.Split(';');
                }
                // 设置数据据权限范围
                ServiceManager.Instance.PermissionService.GrantRoleOrganizeScope(UserInfo, targetRoleId, this.TargetPermissionId, grantOrganizeIds);
                ServiceManager.Instance.PermissionService.RevokeRoleOrganizeScope(UserInfo, targetRoleId, this.TargetPermissionId, revokeOrganizeIds);
            }
            return true;
        }

        private void btnBatchSave_Click(object sender, EventArgs e)
        {
            // 设置鼠标繁忙状态
            this.Cursor = Cursors.WaitCursor;
            try
            {
                // 批量保存
                if (this.BatchSave())
                {
                    if (BaseSystemInfo.ShowInformation)
                    {
                        // 批量保存，进行提示
                        MessageBox.Show(AppMessage.MSG0011, AppMessage.MSG0000, MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
            }
            catch (Exception ex)
            {
                this.ProcessException(ex);
            }
            finally
            {
                // 设置鼠标默认状态
                this.Cursor = Cursors.Default;
            }
        }

        private void grdRole_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            //定义一个画笔，颜色用行标题的前景色填充
            SolidBrush brush = new SolidBrush(this.grdRole.RowHeadersDefaultCellStyle.ForeColor);
            //得到当前行的行号
            int rowIndex = e.RowIndex + 1;
            //DataGridView的RowHeadersWidth 为了算中间位置
            int rowHeadersWidth = this.grdRole.RowHeadersWidth;
            //根据宽度与显示的字符数计算中间位置
            int rowHeadersX = (rowHeadersWidth - rowIndex.ToString().Length * 6) / 2;
            int rowHeadersY = e.RowBounds.Location.Y + 4;
            e.Graphics.DrawString((rowIndex).ToString(System.Globalization.CultureInfo.CurrentUICulture), this.grdRole.DefaultCellStyle.Font, brush, rowHeadersX, rowHeadersY);
        }

        private bool IsANonHeaderButtonCell(DataGridViewCellEventArgs cellEvent)
        {
            if (this.grdRole.Columns[cellEvent.ColumnIndex] is DataGridViewButtonColumn && cellEvent.RowIndex != -1)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        private void grdRole_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (IsANonHeaderButtonCell(e))
            {
                string userId = BaseInterfaceLogic.GetDataGridViewEntityID(this.grdRole, BaseUserTable.FieldId);
                if (!string.IsNullOrEmpty(userId) && !string.IsNullOrEmpty(this.TargetPermissionId))
                {
                    FrmRolePermissionScope frmRolePermissionScope = new FrmRolePermissionScope(userId, null, this.TargetPermissionId, null);
                    frmRolePermissionScope.ShowDialog(this);
                }
            }
            // 保证只能勾选一个
            else if (this.grdRole.Rows[e.RowIndex].Cells[e.ColumnIndex] is DataGridViewCheckBoxCell)
            {
                Boolean bl = Convert.ToBoolean(this.grdRole.Rows[e.RowIndex].Cells[e.ColumnIndex].EditedFormattedValue);
                if (bl)
                {
                    for (int i = 0; i < e.ColumnIndex; i++)
                    {
                        if (this.grdRole.Rows[e.RowIndex].Cells[i] is DataGridViewCheckBoxCell)
                        {
                            this.grdRole.Rows[e.RowIndex].Cells[i].Value = !bl;
                        }
                    }
                    this.grdRole.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = bl;
                    for (int i = e.ColumnIndex + 1; i < grdRole.ColumnCount; i++)
                    {
                        if (this.grdRole.Rows[e.RowIndex].Cells[i] is DataGridViewCheckBoxCell)
                        {
                            this.grdRole.Rows[e.RowIndex].Cells[i].Value = !bl;
                        }
                    }
                }
            }
        }
    }
}